package com.base.common.taglib;

import java.io.IOException;

import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.FaceletException;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagHandler;
import com.sun.facelets.tag.jsf.ComponentConfig;

/**
 * 根据传进来的参数值进行判断，如果是true就显示，如果是false，就不显示
 * 
 * 
 */
public class YesNoGrantedTag extends TagHandler {
	private final TagAttribute test;

	public YesNoGrantedTag(ComponentConfig componentConfig) {
		super(componentConfig);
		this.test = this.getAttribute("test");
	}

	@Override
	public void apply(FaceletContext faceletContext, UIComponent uiComponent)
			throws IOException, FacesException, FaceletException, ELException {
		if (this.test != null) {
			ValueExpression ve = this.test.getValueExpression(faceletContext,
					Object.class);
			if (ve != null) {
				Object obj = ve.getValue(faceletContext);
				if (obj != null && obj instanceof Boolean) {
					if ((Boolean) obj) {
						this.nextHandler.apply(faceletContext, uiComponent);
					}
				}
			}
		}
	}
}
